﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PC.Client
{
    public class ProcessHelper
    {
        private const uint WM_SYSCOMMAND = 0x112;                    //系统消息
        private const int SC_MONITORPOWER = 0xF170;                  //关闭显示器的系统命令
        private const int MonitorPowerOff = 2;                       //2为PowerOff, 1为省电状态，-1为开机
        private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);//广播消息，所有顶级窗体都会接收

        [DllImport("User32.DLL")]
        public static extern void LockWorkStation(); //锁屏
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, uint wParam, int IParam);

        /// <summary>
        /// 关机
        /// </summary>
        public static void ShutDown() 
        {
            Process.Start("shutdown", "-s -t 3");
        }

        /// <summary>
        /// 重启
        /// </summary>
        public static void Restart()
        {
            Process.Start("shutdown", "-r -t 3");
        }

        /// <summary>
        /// 注销
        /// </summary>
        public static void Logout()
        {
            Process.Start("shutdown", "-l ");
        }

        /// <summary>
        /// 锁定
        /// </summary>
        public static void Lock()
        {
            LockWorkStation();
        }

        /// <summary>
        /// 截图
        /// </summary>
        public static Bitmap ScreenShot()
        {
            //获取屏幕长宽
            Rectangle ScreenArea = Screen.AllScreens[0].Bounds;
            Bitmap bitmap = new Bitmap(ScreenArea.Width, ScreenArea.Height);
            //屏幕页面封装GDI+ 
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.CopyFromScreen(0, 0, 0, 0, ScreenArea.Size);

            return bitmap;
        }

        /// <summary>
        /// 关闭显示器
        /// </summary>
        public static void CloseMonitor()
        {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);
        }

    }
}
